﻿using FRMS.BUS;
using FRMS.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class MenuController : ControllerBase
    {
        private JwtSettings _jwtSettings;
        IHttpContextAccessor _access;
        public MenuController(IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access
         )
        {
            JwtSettings jwtSettings = new JwtSettings();
            jwtSettings.Audience = AppSettingsHelper.Configuration["JwtSettings:Audience"];
            jwtSettings.Issuer = AppSettingsHelper.Configuration["JwtSettings:Issuer"];
            jwtSettings.SecretKey = AppSettingsHelper.Configuration["JwtSettings:SecretKey"];
            _jwtSettings = jwtSettings;
            //_jwtSettings = jwtSettingsAccesser.Value;
            _access = access;
        }

        /// <summary>
        /// 新增菜单
        /// </summary>
        [HttpPost]
        [Route("AddMenu")]
        public ResponseMessage AddMenu(MenuInfo info)
        {
            ResponseMessage message = new ResponseMessage();
            MenuBUS BUS = new MenuBUS();
            try
            {
                message = BUS.AddMenu(info);

            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "新增菜单";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "新增菜单异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        [HttpPost]
        [Route("EditMenu")]
        public ResponseMessage EditMenu(MenuInfo info)
        {
            ResponseMessage message = new ResponseMessage();
            MenuBUS BUS = new MenuBUS();
            try
            {
                message = BUS.EditMenu(info);

            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "修改菜单";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "修改菜单异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        [HttpPost]
        [Route("DeleteMenu")]
        public ResponseMessage DeleteMenu(MenuInfo info)
        {
            ResponseMessage message = new ResponseMessage();
            MenuBUS BUS = new MenuBUS();
            try
            {
                message = BUS.DeleteMenu(info);

            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "删除菜单";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "删除菜单异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 查询菜单
        /// </summary>
        [HttpPost]
        [Route("QueryMenu")]  
        public ResponseMessage QueryMenu(MenuInfo info)
        {
            ResponseMessage message = new ResponseMessage();
            MenuBUS BUS = new MenuBUS();
            try
            {
                message = BUS.QueryMenu(info);

            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询菜单";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询菜单异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 载入菜单
        /// </summary>
        [HttpPost]
        [Route("LoadMenu")]
        public ResponseMessage LoadMenu(MenuInfo info)
        {
            ResponseMessage message = new ResponseMessage();
            MenuBUS BUS = new MenuBUS();
            try
            {
                message = BUS.LoadMenu(info);

            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "载入菜单异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 查询所有菜单
        /// </summary>
        [HttpPost]
        [Route("QueryAllMenu")]
        public ResponseMessage QueryAllMenu()
        {
            ResponseMessage message = new ResponseMessage();
            MenuBUS BUS = new MenuBUS();
            try
            {
                message = BUS.QueryAllMenu();

            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询所有菜单异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }
    }
}
